package com.medicine.manage.service.block.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.medicine.common.core.domain.entity.SysDept;
import com.medicine.common.core.domain.entity.SysUser;
import com.medicine.manage.block.WeBASESignUtil;
import com.medicine.manage.block.WeBASEUtils;
import com.medicine.manage.block.domain.UserExtend;
import com.medicine.manage.block.mapper.UserExtendMapper;
import com.medicine.manage.block.service.IUserExtendService;
import com.medicine.manage.service.block.IBlockService;
import com.medicine.system.service.ISysDeptService;
import com.medicine.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BlockServiceImpl implements IBlockService {

    @Autowired
    private WeBASESignUtil weBASESignUtil;

    @Autowired
    private WeBASEUtils weBASEUtils;

    @Autowired
    private UserExtendMapper userExtendMapper;

    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private IUserExtendService userExtendService;




    /**
     * 创建普通用户并注册
     * @param signUser
     * @return
     */
    public UserExtend createNormalSignUser(Long userId,String signUser) {
        UserExtend userExtend =  weBASESignUtil.createSignUser(signUser);
        userExtend.setUserId(userId);
        userExtend.setUserName(signUser);
        userExtendService.insertUserExtend(userExtend);
        return userExtend;
    }

    public UserExtend createAdminSignUser(Long userId,String signUser) {
        UserExtend userExtend =  weBASESignUtil.createSignUser(signUser);
        userExtend.setUserId(userId);
        userExtend.setUserName(signUser);
        userExtendService.insertUserExtend(userExtend);
        return userExtend;

    }

    public UserExtend createEmployeeSignUser(Long deptId, Long userId,String signUser) {
        UserExtend userExtend =  weBASESignUtil.createSignUser(signUser);
        userExtend.setUserId(userId);
        userExtend.setUserName(signUser);
        userExtendService.insertUserExtend(userExtend);
        System.out.println("签名服务调用成功");
        System.out.println(userExtend);
        // 在区块链上注册操作员权限
        List param = new ArrayList();
        param.add(userExtend.getAddress());
        SysDept sysDept =  sysDeptService.selectDeptById(deptId);

        UserExtend adminUser =userExtendService.getAdminUserByDeptId(deptId);


        System.out.println("合约调用");
        System.out.println(adminUser.getUserName() + "    " + sysDept.getContractAddress());
        String resultStr = weBASEUtils.funcPost(adminUser.getUserName(),  sysDept.getContractAddress() ,WeBASEUtils.SET_OPTION_ROLE, param);
        System.out.println(resultStr);
        JSONObject result = JSON.parseObject(resultStr);
        if(!result.getBoolean("statusOK")){
            throw new RuntimeException("用户" + signUser + "在区块链中注册操作员失败， 失败原因：" + result.getString("message"));
        }
        return userExtend;
    }




    public String deployContract(String signUserId){

        return weBASEUtils.deploy(signUserId,"",new ArrayList());
    }
}
